/**
 * introduce : vue指令配置文件
 * author    : yhy
 * date      : 2018/07/02
 */
export default {
  install (Vue, options) {
    //  添加全局指令
    Vue.directive('clickoutside', {
      bind: (el, binding, vnode) => {
        function documentHanler (e) {
          if (el.contains(e.target)) {
            return false
          }
          if (binding.expression) {
            binding.value(e)
          }
        }
        document.addEventListener('click', documentHanler)
        el._vueClick = documentHanler
      },
      unbind: (el, binding) => {
        document.removeEventListener('click', el._vueClick)
        delete el._vueClick
      }
    })

    // 注册一个全局自定义指令 `v-focus`
    Vue.directive('focus', {
      // 当被绑定的元素插入到 DOM 中时……
      inserted: function (el) {
        // 聚焦元素
        el.focus()
      }
    })
  }
}
